package com.zhengtoudai.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.BaseActivity;
import com.zhengtoudai.http.HttpUtil;
import com.zhengtoudai.injector.component.DaggerActivityComponent;
import com.zhengtoudai.injector.module.ActivityModule;
import com.zhengtoudai.ui.bean.MyFragmentModel;
import com.zhengtoudai.ui.bean.Result;
import com.zhengtoudai.ui.presenter.MyFragmentPresenter;
import com.zhengtoudai.ui.presenter.ShengTenpayPresenterImpl;
import com.zhengtoudai.ui.presenter.VerificationCodePresenterImpl;
import com.zhengtoudai.ui.view.MyFragmentView;
import com.zhengtoudai.ui.view.ShengTenpeyView;
import com.zhengtoudai.ui.view.VerificationCodeView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.util.Tool;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithDrawActivity extends BaseActivity implements ShengTenpeyView,VerificationCodeView,MyFragmentView{

    @Bind(R.id.tv_my_balance)
    TextView mTvMyBalance;
    @Bind(R.id.edt_withdraw_money)
    EditText mEdtWithdrawMoney;
    @Bind(R.id.tv_mobile_phone)
    TextView mTvMobilePhone;
    @Bind(R.id.edt_verification_code)
    EditText mEdtVerificationCode;
    @Bind(R.id.tv_verification_code)
    TextView mTvVerificationCode;
   /* @Bind(R.id.tv_handling_charge)
    TextView mTvHandlingCharge;*/

    @Inject
    ShengTenpayPresenterImpl mPresenter;

    @Inject
    VerificationCodePresenterImpl mCodePresenter;

    private MyFragmentPresenter mMyPresenter;

    private MyFragmentModel mWithdrawInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_with_draw);
        ButterKnife.bind(this);
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitle("提现");
    }

    @Override
    protected void initView() {
        DaggerActivityComponent.builder().appComponent(App.mApp.getAppComponent())
                .activityModule(new ActivityModule(this))
                .build()
                .inject(this);
        mPresenter.attachView(this);
        mCodePresenter.attachView(this);
        mMyPresenter=new MyFragmentPresenter(this);
        mMyPresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
        mCodePresenter.detachView();
        mMyPresenter.detachView();
    }

    @Override
    public void showDialog() {
        showProgressDialog("正在跳转中...");
    }

    @Override
    public void onFailure(Throwable e) {
        HttpUtil.disposeOnFailure(context,e);
    }
    @Override
    public void onResult(String html) {
        Bundle bundle=new Bundle();
        bundle.putString("title","盛付通");
        bundle.putString("html",html);
        startActivity(bundle,WebViewActivity.class);
    }

    @Override
    public void dismissDialog() {
        dismissProgressDialog();
    }

    @OnClick({R.id.tv_verification_code, R.id.btn_withdraw})
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        mEdtWithdrawMoney.setError(null);
        mEdtVerificationCode.setError(null);
        if (mWithdrawInfo==null){
            AbToastUtil.showToast(context,"正在获取提现信息...");
            mMyPresenter.loadData(context);
            return;
        }
        switch (view.getId()) {
            case R.id.tv_verification_code:
                    mCodePresenter.getSMSCode(mWithdrawInfo.getResult().getUserPhone());
                break;
            case R.id.btn_withdraw:
                String money=mEdtWithdrawMoney.getText().toString();
                String code=mEdtVerificationCode.getText().toString();
                if (TextUtils.isEmpty(money)||Double.parseDouble(money)<100){
                    mEdtWithdrawMoney.setError("提现金额不得小于100元");
                    mEdtWithdrawMoney.requestFocus();
                }else if (Double.parseDouble(money)>Double.parseDouble(mWithdrawInfo.getResult().getAvailableMoney())){
                    mEdtWithdrawMoney.setError("提现金额不得大于可提现金额");
                    mEdtWithdrawMoney.requestFocus();
                }else if (code==null){
                    mEdtVerificationCode.setError("请输入手机验证码");
                    mEdtVerificationCode.requestFocus();
                }else{
                    Map<String,String> params=new HashMap<>();
                    params.put("verify",code);
                    params.put("withdrawMoney",money);
                    mPresenter.postShengTenpay("appAccountCorrelation/tgWithdraw.app",params);
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mMyPresenter!=null) {
            mMyPresenter.loadData(context);
        }
    }

    @Override
    public void getDataSuccess(MyFragmentModel model) {
        if (model.getCode().equals("200")){
            mWithdrawInfo=model;
            mTvMyBalance.setText(model.getResult().getAvailableMoney());
            mTvMobilePhone.setText(Tool.changeMobile(model.getResult().getUserPhone()));
        }else{
            AbToastUtil.showToast(context,model.getMessage());
        }
    }

    @Override
    public void getDataFail(String msg) {
        AbToastUtil.showToast(context,msg);
    }

    @Override
    public void showLoading() {
        showProgressDialog("正在加载中...");
    }

    @Override
    public void hideLoading() {
        dismissProgressDialog();
    }

    @Override
    public void showSMSDialog() {
        showProgressDialog("正在发送验证码...");
    }

    @Override
    public void onSMSResult(Result result) {
        if (result.getCode()==200){
            Tool.countTime(60,mTvVerificationCode);
        }
        AbToastUtil.showToast(context,result.getMessage());
    }

    @Override
    public void onSMSFailure(Throwable e) {
        HttpUtil.disposeOnFailure(context,e);
    }

    @Override
    public void dismissSMSDialog() {
        dismissProgressDialog();
    }
}
